//
//  RecordViewController.m
//  SugarPile
//
//  Created by LC on 16/2/29.
//  Copyright © 2016年 www.lanou39.蓝鸥科技. All rights reserved.
//

#import "RecordViewController.h"
#import "DataBaseManager.h"
#import "History.h"
#import "HotDetailsViewController.h"
@interface RecordViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) UITableView *collecttableView;
@property (nonatomic, retain) NSMutableArray *arr; /**< 数据库数组 */

@end

@implementation RecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    // 打开表
//    DataBaseManager *dbManger = [DataBaseManager shareInstance];
//    [dbManger openDB2];
    NSMutableArray *arr = [[DataBaseManager shareInstance] Historyselect];
    NSArray *dataArr = [[arr reverseObjectEnumerator] allObjects];
    self.arr = [NSMutableArray arrayWithArray:dataArr];
    NSLog(@"%@", _arr);
    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"清除记录" style:UIBarButtonItemStylePlain target:self action:@selector(qingchu)] autorelease];

    
    self.collecttableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
    _collecttableView.delegate = self;
    _collecttableView.dataSource = self;
    [self.view addSubview:_collecttableView];
    [self.collecttableView release];
    [_collecttableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _arr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    Movie *m = _arr[indexPath.row];
    cell.textLabel.text = m.movieName;
    
    return cell;
}
- (void)qingchu
{
    // 提示控制器
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"清空历史么 亲" message:@"确认删除么 亲" preferredStyle:UIAlertControllerStyleAlert];
    
    
    
    [alertController addAction:[UIAlertAction actionWithTitle:@"好!" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        
        // 清除数据库中文件
        for (History *m in _arr) {
            [[DataBaseManager shareInstance] deleteHistory:m];
        }
        // 清空数组
        [_arr removeAllObjects];
        [_collecttableView reloadData];
        
    }]];
    [alertController addAction:[UIAlertAction actionWithTitle:@"再等等" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        
    }]];
    
    [self presentViewController:alertController animated:YES completion:nil];
}
// 点击跳转方法
- (void)tableView:(UITableView *)tableView didHighlightRowAtIndexPath:(nonnull NSIndexPath *)indexPath
{
    
    HotDetailsViewController *hotVC = [[[HotDetailsViewController alloc] init] autorelease];
    hotVC.hidesBottomBarWhenPushed = YES;
    Movie *m = _arr[indexPath.row];
    // 字符串转NSNumber
    NSNumberFormatter *f = [[NSNumberFormatter alloc] init];
    id result = [f numberFromString:m.pubdate];
    
    hotVC.detailsId = result;
    [self.navigationController pushViewController:hotVC animated:YES];
}

// 允许编辑
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}
// 通过编辑状态判断编辑操作
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    // 通过参数editingStyle判断 执行不同操作
    // 删除操作
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // 删除 分两个部分 数据/视图
        // 先操作数据 再同步更新视图
        // 1.从数组中 删除数据
        History *m = _arr[indexPath.row];
        [[DataBaseManager shareInstance] deleteHistory:m];
        [_arr removeObjectAtIndex:indexPath.row];
        NSLog(@"%@", _arr);
        // 2.同步视图
        // 参数1: 试图操作cell 的位置
        // 参数2: 操作的动画选项
        [_collecttableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
    }
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    DataBaseManager *dbManger = [DataBaseManager shareInstance];
//    [dbManger openDB2];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
